/*
 * DConfirmaColumnas.java
 *
 * Created on 29 de agosto de 2007, 03:10 PM
 */
package ig.formato.gui.app;

import ig.formato.basic.ManipulacionDatos;
import ig.formato.modelo.IMReporte;
import ig.formato.util.IGFormatoException;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JOptionPane;

/**
 *
 * @author  jsanchez
 */
public class DConfirmaColumnas extends javax.swing.JDialog {

    /** Creates new form DConfirmaColumnas */
    IMReporte modelo = null;
    java.awt.Frame parent=null;
    public DConfirmaColumnas(java.awt.Frame parent, Collection data, IMReporte modelo) {
        super(parent, true);
        this.parent=parent;
        this.modelo = modelo;
        initComponents();

        Object[][] _data = new Object[data.size()][2];
        int i = 0;
        this.CCampos.removeAllItems();
        this.CCampos.addItem("<NINGUNO>");
        for (Object elem : data) {
            this.CCampos.addItem(elem);
            _data[i][0] = elem;
            _data[i][1] = true;
            i++;
        }
        this.CCampos.setSelectedItem("<NINGUNO>");
        boolean[] edit = {false, true};
        String[] col = {"Columnas", "Mostrar"};
        this.Tabla.setModel(new ig.formato.util.MyTableModel(_data, col, edit));
        this.Tabla.repaint();
        this.Tabla.updateUI();
        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        CTodasNinguna = new javax.swing.JCheckBox();
        Aceptar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        CCampos = new javax.swing.JComboBox();
        CBSaltarPagina = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.jLabel1.text")); // NOI18N

        Tabla.setAutoCreateRowSorter(true);
        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "<LIMPIA>"
            }
        ));
        jScrollPane1.setViewportView(Tabla);

        CTodasNinguna.setFont(new java.awt.Font("Dialog", 1, 10));
        CTodasNinguna.setSelected(true);
        CTodasNinguna.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.CTodasNinguna.text")); // NOI18N
        CTodasNinguna.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        CTodasNinguna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CTodasNingunaActionPerformed(evt);
            }
        });
        CTodasNinguna.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                CTodasNingunaStateChanged(evt);
            }
        });

        Aceptar.setFont(new java.awt.Font("Dialog", 1, 10));
        Aceptar.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.Aceptar.text")); // NOI18N
        Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarActionPerformed(evt);
            }
        });

        Cancelar.setFont(new java.awt.Font("Dialog", 1, 10));
        Cancelar.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.Cancelar.text")); // NOI18N
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 10));
        jLabel2.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.jLabel2.text")); // NOI18N

        CCampos.setFont(new java.awt.Font("Dialog", 0, 10));

        CBSaltarPagina.setFont(new java.awt.Font("Dialog", 0, 10));
        CBSaltarPagina.setText(org.openide.util.NbBundle.getMessage(DConfirmaColumnas.class, "DConfirmaColumnas.CBSaltarPagina.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(CCampos, 0, 204, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(CBSaltarPagina)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Aceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Cancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, CTodasNinguna, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(CTodasNinguna)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Aceptar)
                    .add(CBSaltarPagina)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(CCampos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(Cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void CTodasNingunaStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_CTodasNingunaStateChanged
        for (int i = 0; i < ((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData().length; i++) {
            this.Tabla.setValueAt(CTodasNinguna.isSelected(), i, 1);
        }
        this.Tabla.repaint();
        this.Tabla.updateUI();
    }//GEN-LAST:event_CTodasNingunaStateChanged

    private void CTodasNingunaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CTodasNingunaActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_CTodasNingunaActionPerformed

    private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarActionPerformed
        String SP = this.CCampos.getSelectedItem().toString().trim();
        boolean isSaltoPagina = false;
        Collection c = new ArrayList();
        for (int i = 0; i < ((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData().length; i++) {
            if ((Boolean) ((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData()[i][1]) {
                c.add(((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData()[i][0]);
            }
            if (SP != null && ((Boolean) ((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData()[i][1]) && SP.equalsIgnoreCase(((ig.formato.util.MyTableModel) this.Tabla.getModel()).getData()[i][0].toString().trim())) {
                if (!this.CCampos.getSelectedItem().toString().equals("<NINGUNO>")) {
                    modelo.setSaltoDePagina(this.CCampos.getSelectedItem().toString());
                    modelo.setSaltarPagina(this.CBSaltarPagina.isSelected());
                    isSaltoPagina = true;
                } else {
                    modelo.setSaltoDePagina(null);
                    modelo.setSaltarPagina(null);
                }
            }
        }
        if (isSaltoPagina || SP.equalsIgnoreCase("<NINGUNO>")) {
            modelo.setColumnasAProcesar(c);
            this.dispose();
        }else if(!SP.equalsIgnoreCase("<NINGUNO>")){
            ManipulacionDatos.showMensajeFrama(new IGFormatoException("El campo \""+SP+"\" no puede ser utilizado como salto de pagina\npor NO que aparece seleccinado en la tabla de campos a procesar","Seleccione en la tabla el campo"), this.parent);
        }else if(c.size()==0){
            ManipulacionDatos.showMensajeFrama(new IGFormatoException("No se ha seleccionado ningun campo para procesar el informe","Seleccione los campos en tabla"), this.parent);
        }
    }//GEN-LAST:event_AceptarActionPerformed

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        this.dispose();// TODO add your handling code here:
    }//GEN-LAST:event_CancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Aceptar;
    private javax.swing.JCheckBox CBSaltarPagina;
    private javax.swing.JComboBox CCampos;
    private javax.swing.JCheckBox CTodasNinguna;
    private javax.swing.JButton Cancelar;
    private javax.swing.JTable Tabla;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
